package org.bagou.xiangs.framework_mvvm.newer;

import android.content.Context;
import android.content.res.AssetManager;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ViewModelMvvm extends ViewModel {

    protected LiveData<WechatInfo> getUserInfoPromote(Context context) {

        MutableLiveData<WechatInfo> liveData = new MutableLiveData<>();
        WechatInfo info = getWechatInfo(context);
        liveData.postValue(info);
        return liveData;
    }

    private WechatInfo getWechatInfo(Context context) {
        AssetManager assetManager = context.getAssets();
        try {
            InputStreamReader reader = new InputStreamReader(assetManager.open("mvvm.json"));
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line;
            StringBuffer stringBuffer = new StringBuffer();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
            }
            bufferedReader.close();
            reader.close();

            Gson gson = new Gson();
            WechatInfo wechatInfo = gson.fromJson(stringBuffer.toString(), WechatInfo.class);
            return wechatInfo;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }
}
